<?php

namespace App\Models;

use App\Traits\External;
use Illuminate\Auth\Authenticatable;
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Laravel\Lumen\Auth\Authorizable;
use DateTimeInterface;

class Crm_Member_Annex extends Model implements AuthenticatableContract, AuthorizableContract
{
    use Authenticatable, Authorizable, HasFactory;
    /**
     * 关联到模型的数据表
     *
     * @var string
     */
    protected $table = 'crm_member_annex';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    protected $fillable = [
        'agencyid','mid','qq','email','tel','faith','sin','debt',
        'familyback','parentstatus','parenteconomic','sibling','allopatry',
        'wedtime','wantchild','focus','liveparent'
    ];

    protected $appends = [
        'faithText','sinText','debtText','familybackText','parentstatusText','parenteconomicText',
        'siblingText','allopatryText','wedtimeText','wantchildText','focusText','liveparentText',
    ];

    public function getFaithTextAttribute()
    {
        if (!empty($this->faith)){
            $faithArr = External::getParams('faith');
            return $faithArr[$this->faith];
        }else{
            return '无';
        }
    }

    public function getSinTextAttribute()
    {
        if (!empty($this->sin)){
            $sinArr = External::getParams('sin');
            return $sinArr[$this->sin];
        }else{
            return '无';
        }
    }

    public function getDebtTextAttribute()
    {
        if (!empty($this->debt)){
            $debtArr = External::getParams('debt');
            return $debtArr[$this->debt];
        }else{
            return '无';
        }
    }

    public function getFamilybackTextAttribute()
    {
        if (!empty($this->familyback)){
            $familybackArr = External::getParams('familyback');
            return $familybackArr[$this->familyback];
        }else{
            return '无';
        }
    }

    public function getParentstatusTextAttribute()
    {
        if (!empty($this->parentstatus)){
            $parentstatusArr = External::getParams('parentstatus');
            return $parentstatusArr[$this->parentstatus];
        }else{
            return '无';
        }
    }

    public function getParenteconomicTextAttribute()
    {
        if (!empty($this->parenteconomic)){
            $parenteconomicArr = External::getParams('parenteconomic');
            return $parenteconomicArr[$this->parenteconomic];
        }else{
            return '无';
        }
    }

    public function getSiblingTextAttribute()
    {
        if (!empty($this->sibling)){
            $siblingArr = External::getParams('sibling');
            return $siblingArr[$this->sibling];
        }else{
            return '无';
        }
    }

    public function getAllopatryTextAttribute()
    {
        if (!empty($this->allopatry)){
            $allopatryArr = External::getParams('allopatry');
            return $allopatryArr[$this->allopatry];
        }else{
            return '无';
        }
    }

    public function getWedtimeTextAttribute()
    {
        if (!empty($this->wedtime)){
            $wedtimeArr = External::getParams('wedtime');
            return $wedtimeArr[$this->wedtime];
        }else{
            return '无';
        }
    }

    public function getWantchildTextAttribute()
    {
        if (!empty($this->wantchild)){
            $wantchildArr = External::getParams('wantchild');
            return $wantchildArr[$this->wantchild];
        }else{
            return '无';
        }
    }

    public function getFocusTextAttribute()
    {
        if (!empty($this->focus)){
            $focusArr = External::getParams('focus');
            return $focusArr[$this->focus];
        }else{
            return '无';
        }
    }

    public function getLiveparentTextAttribute()
    {
        if (!empty($this->liveparent)){
            $liveparentArr = External::getParams('liveparent');
            return $liveparentArr[$this->liveparent];
        }else{
            return '无';
        }
    }

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }

    public static function getFilter($filter)
    {
        return self::where(function ($query) use ($filter){
            if (isset($filter['username']) && !empty($filter['username'])) {
                $query->where('username', 'LIKE', '%' . $filter['username'] . '%');
            }

            if (isset($filter['mobile']) && !empty($filter['mobile'])) {
                $query->where('mobile', 'LIKE', '%' . $filter['mobile'] . '%');
            }

            if (isset($filter['gender']) && !empty($filter['gender'])) {
                $query->where('gender', $filter['gender']);
            }

            if (isset($filter['max_age']) && !empty($filter['max_age'])) {
                $query->where('age', '<=', $filter['max_age']);
            }

            if (isset($filter['min_age']) && !empty($filter['min_age'])) {
                $query->where('age', '>=',$filter['min_age']);
            }

            if (isset($filter['marry']) && !empty($filter['marry'])) {
                $query->where('marry', $filter['marry'] );
            }

            if (isset($filter['education']) && !empty($filter['education'])) {
                $query->where('education',  $filter['mobile'] );
            }

            if (isset($filter['state']) && !empty($filter['state'])) {
                $query->where('state', $filter['state']);
            }

            if (isset($filter['makerid']) && !empty($filter['makerid'])) {
                $query->where('makerid', $filter['makerid']);
            }

            if (isset($filter['bind']) && !empty($filter['bind'])) {
                $query->where('bind', $filter['bind']);
            }

            if (isset($filter['inputid']) && !empty($filter['inputid'])) {
                $query->where('inputid', $filter['inputid']);
            }

            if (isset($filter['sourcefrom']) && !empty($filter['sourcefrom'])) {
                $query->where('sourcefrom', $filter['sourcefrom']);
            }

            if (isset($filter['intention']) && !empty($filter['intention'])) {
                $query->where('intention', $filter['intention']);
            }

            $query->where('agencyid', $filter['aid']);

        })->orderBy($filter['order'],$filter['sort']);

    }


}
